package top.houry.linklist;

/**
 * @Desc
 * @Author houruiyang
 * @Date 2022/9/20
 **/
public class WoodCut {

    public static void main(String[] args) {
        int[] l = {232, 124, 456};
        int k = 7;
        int[] l2 = {1,2,3};
        System.out.println(woodCount(l, k));
        System.out.println(woodCount(l2, k));
    }


    public static int woodCount(int[] l, int k) {
        if (null == l || l.length <=0 ) {
            return 0;
        }
        if (k <=0) {
            return 0;
        }

        int left = 1;
        int right = 0;

        for (int length : l) {
            right = Math.max(right, length);
        }


        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (count(l, mid) >= k) {
                left = mid;
            } else {
                right = mid;
            }
        }
        if (count(l, right) >= k) {
            return right;
        }

        if (count(l, left) >= k) {
            return left;
        }

        return 0;



    }



    public static int count(int[] l, int length) {
        int pieces = 0;
        for (int len : l) {
            pieces += len / length;
        }
        return pieces;
    }

}
